package socket;

import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.net.Socket;

public class Client{
	
	private String host;
	
	private int port;
	
	private Socket socket;
	
	Client(String host, int port, SocketBuilder builder){

		if(host != null){
			
			if(port > 0){
				
				try {
					
					this.socket = builder.buildSocket(host,port);
					
					this.host = host;
					
					this.port = port;
				
				} catch (Exception e) {
					
					throw new IllegalArgumentException(String.format("Could not connect with socket Host: %s - Port: %s", host, port), e);
				}
			}
			else {
				throw new IllegalArgumentException(String.format("Port invalid value: %s", port));
			}
		}
		else throw new NullPointerException("Host value cannot be null");
	}

	public String getHost(){
		return host;
	}
	
	public int getPort(){
		return port;
	}
	
	public Client send(Serializable obj){
		
		if(obj != null){
		
			try{
			
				ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
			
				out.writeObject(obj);
		    
				return this;
				
			} catch (Exception e) {
				
				throw new IllegalArgumentException(String.format("Could not send Object: %s", obj), e);
			}
		}else{
			
			throw new NullPointerException("Object cannot be null");
		}
	}
	
	
	
	
	public static void main(String[] args){	
		
		Sockets.newSSLClient().send("HOLA HOLA HOLA");
	}
}
